package com.ruoyi.energy.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.energy.domain.NyTrans;
import com.ruoyi.energy.domain.vo.NyTransExcelVO;
import com.ruoyi.energy.service.INyTransService;
import com.ruoyi.system.api.RemoteBmCodeService;
import com.ruoyi.system.api.domain.RemoteSysBmType;
import com.ruoyi.system.api.enums.SysBmCodeTypeValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 变压器台账Controller
 *
 * @author cjie
 * @date 2021-10-11
 */
@Api(tags = " 变压器列表")
@RestController
@RequestMapping("/nyTrans")
public class NyTransController extends BaseController {
    @Autowired
    private INyTransService nyTransService;
    @Autowired
    private RemoteBmCodeService remoteBmCodeService;

    /**
     * 查询变压器台账列表,wx
     */
    @ApiOperation(value="查询变压器台账列表", notes="查询变压器台账列表")
    @PreAuthorize(hasPermi = "energy:nyTrans:list")
    @GetMapping("/list")
    public TableDataInfo list(NyTrans nyTrans) {
        startPage();
        List<NyTrans> list = nyTransService.selectNyTransList(nyTrans);
        return getDataTable(list);
    }

    /**
     * 导出变压器台账列表
     */
    @ApiOperation(value="导出变压器台账列表", notes="导出变压器台账列表")
    @PreAuthorize(hasPermi = "energy:nyTrans:export")
    @Log(title = "变压器台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NyTrans nyTrans) throws IOException {
        List<NyTrans> list = nyTransService.selectNyTransList(nyTrans);
        ExcelUtil<NyTrans> util = new ExcelUtil<NyTrans>(NyTrans.class);
        util.exportExcel(response, list, "变压器台账数据");
    }
    /**
     * 获取变压器台账详细信息
     */
    @ApiOperation(value="获取变压器台账详细信息", notes="获取变压器台账详细信息")
    @PreAuthorize(hasPermi = "energy:nyTrans:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(nyTransService.selectNyTransById(id));
    }

    /**
     * 微信获取变压器详细信息
     */
    @ApiOperation(value="微信获取变压器台账详细信息", notes="微信获取变压器台账详细信息")
    @PreAuthorize(hasPermi = "energy:nyTrans:query")
    @GetMapping(value = "/wx/{id}")
    public AjaxResult getWXInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(nyTransService.selectWXNyTransDetialById(id));
    }

    /**
     * 新增变压器台账
     */
    @ApiOperation(value="新增变压器台账", notes="新增变压器台账")
    @PreAuthorize(hasPermi = "energy:nyTrans:add")
    @Log(title = "变压器台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NyTrans nyTrans) {
        RemoteSysBmType remoteSysBmType= remoteBmCodeService.genCode(nyTrans.getSysBmId(), SysBmCodeTypeValue.DEVICE);
        if(remoteSysBmType != null){
            String nowCode = remoteSysBmType.getNowCode();//生成的唯一编码
            String[] deviceType = remoteSysBmType.getHierarchy().split("-");//层级
            nyTrans.setNum(nowCode);
            nyTrans.setDeviceType(deviceType[deviceType.length-1]);
        }
        return toAjax(nyTransService.insertNyTrans(nyTrans));
    }

    /**
     * 修改变压器台账
     */
    @ApiOperation(value="修改变压器台账", notes="修改变压器台账")
    @PreAuthorize(hasPermi = "energy:nyTrans:edit")
    @Log(title = "变压器台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NyTrans nyTrans) {
        return toAjax(nyTransService.updateNyTrans(nyTrans));
    }

    /**
     * 删除变压器台账
     */
    @ApiOperation(value="删除变压器台账", notes="删除变压器台账")
    @ApiImplicitParam(name = "ids", value = "变压器台账ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "energy:nyTrans:remove")
    @Log(title = "变压器台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(nyTransService.deleteNyTransByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<NyTransExcelVO> util = new ExcelUtil<NyTransExcelVO>(NyTransExcelVO.class);
        util.importTemplateExcel(response, "变压器部件");
    }

    /**
     * 导入抄表记录列表,
     */
    @PreAuthorize(hasPermi = "energy:nyTransPart:edit")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String meterType) throws Exception {
        ExcelUtil<NyTransExcelVO> util = new ExcelUtil<NyTransExcelVO>(NyTransExcelVO.class);
        List<NyTransExcelVO> buiRoomVOList = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = nyTransService.importData(buiRoomVOList,meterType);
        return AjaxResult.success(message);
    }
    
}
